﻿using System;
using System.Drawing;
using System.Windows.Forms;
using PicGame.Res;
using PicGame.Utils;

namespace PicGame.UI
{
    public partial class FormImgs : Form
    {
        private int _row_i = 2;
        private int _col_i = 2;
        private Bitmap _bmp = null;
        private FormMain _frmMain = null;

        public FormImgs(FormMain frmMain)
        {
            this._frmMain = frmMain;
            this._bmp = frmMain.Bmp;

            InitializeComponent();
        }

        private void FormImgs_Load(object sender, EventArgs e)
        {
            this.cmb_RowCount.SelectedIndex = this._row_i;
            this.cmb_ColCount.SelectedIndex = this._col_i;
            this.SetPicBg();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this._frmMain.RowCount = Convert.ToInt32(this.cmb_RowCount.Text);
            this._frmMain.ColCount = Convert.ToInt32(this.cmb_ColCount.Text);
            this._frmMain.Bmp = this._bmp;
            this.DialogResult = DialogResult.OK;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btn_Default_Click(object sender, EventArgs e)
        {
            this._bmp?.Dispose();
            this._bmp = R.Get_Clock();
            this.SetPicBg();
        }

        private void btn_Sel_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    this._bmp?.Dispose();
                    this._bmp = new Bitmap(Image.FromFile(openFile.FileName));
                    this.SetPicBg();
                }
                catch
                {
                    MSGBox.OK("您选择的文件不是图片或文件已损坏！");
                }
            }
        }

        private void cmb_RowCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._row_i = cmb_RowCount.SelectedIndex;
            this.btnOk.Focus();
        }

        private void cmb_ColCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            this._col_i = cmb_ColCount.SelectedIndex;
            this.btnOk.Focus();
        }

        private void SetPicBg()
        {
            this.pic_Img.BackgroundImage?.Dispose();

            this.pic_Img.BackgroundImage = PicGameMain.Instance.GetMinImage(this._bmp, this.pic_Img.Width - 10, this.pic_Img.Height - 10);
        }
    }
}